import 'package:flutter/material.dart';
import 'package:wu_you/data/api/api.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/model/home/composite_model.dart';
import 'package:wu_you/model/home/home_model.dart';
import 'package:package_info/package_info.dart';

class HomeRepository{
  factory HomeRepository() => _getInstance();
  static HomeRepository _instance;

  HomeRepository._internal();

  static HomeRepository _getInstance() {
    if (_instance == null) {
      _instance = HomeRepository._internal();
    }
    return _instance;
  }

  ///获取国籍
  Future<List<LanguageModel>> getLanguages() async {
    List<dynamic> mapResult = await Http(useToken: false).get(Api.languageList);
    List<LanguageModel> lg = mapResult.map((e) => LanguageModel.fromJson(e)).toList();
    return lg;
  }

  ///获取banner
  Future<List<BannerModel>> getBanners() async {
    List<dynamic> mapResult = await Http(useToken: false).get(Api.bannerList);
    List<BannerModel> b = mapResult.map((e) => BannerModel.fromJson(e)).toList();
    return b;
  }

  ///获取平台公告
  static Future<List<NoticeModel>> getSimplePlatformNotice({int page,int limit}) async {
    List<dynamic> mapResult = await Http(useToken: false).get(Api.noticeList,params:{"page":page,"limit":limit});
    List<NoticeModel> nl = mapResult.map((e) => NoticeModel.fromJson(e)).toList();
    return Future.value(nl);
  }

  ///获取最新系统公告
  Future<NoticeModel> getNewsNotice() async {
    dynamic mapResult = await Http(useToken: false).get(Api.noticeNew);
    if (mapResult == null) {
      return null;
    }
    NoticeModel model = NoticeModel.fromJson(mapResult);
    return model;
  }

  ///获取版本更新信息
  Future<NewVersionModel> getAppUpdateInfo() async {
    PackageInfo pi = await PackageInfo.fromPlatform();
    Map<String,dynamic> data = {
      "version":pi.version
    };
    dynamic mapResult = await Http().post(Api.appUpdate,data: data);
    if(mapResult != null){
      return NewVersionModel.fromJson(mapResult);
    }
    return null;
  }

  ///获取版本更新信息
  Future<NewVersionModel> getDownloadUrl() async {
    Map<String,dynamic> data = {
      "version":"1.0.0"
    };
    dynamic mapResult = await Http().post(Api.appUpdate,data: data);
    if(mapResult != null){
      return NewVersionModel.fromJson(mapResult);
    }
    return null;
  }

  ///获取价格
  Future<dynamic> getHomePrice() async {
    dynamic mapResult = await Http().get(Api.moneyPrice);
    return mapResult;
  }
}